package com.cilico.single.shopcart;

import java.util.HashMap;

import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationSet;
import android.view.animation.LinearInterpolator;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.cilico.single.R;
import com.cilico.single.components.Component;
import com.cilico.single.components.CssStyle;
import com.cilico.single.utils.Utils;
import com.cilico.single.view.BaseView;
import com.cilico.single.view.DetailView;
import com.cilico.single.view.FormLayout;
import com.cilico.single.waterfall.Waterfall;

/**
 * 带购物车功能的瀑布流控件
 * @author lvxiangan
 *
 */
public class ShopCart extends RelativeLayout implements Component {

	BaseView bv_;
	public Waterfall waterfall_;
	int width_;
	int height_;
	private ImageView cart_;// 购物车图标
	private ViewGroup anim_mask_layout;// 动画层
	public ImageView rollBall;// 这是在界面上跑的小图片
	public int buyNum = 0;// 购买数量
	private BadgeView buyNumView;// 显示购买数量的控件
	/** 已选菜品（购物车缓存），切勿将其设为null，否则会报空指针异常 */
	public static HashMap<String, Object> order = new HashMap<String, Object>();
	private int blankH_ = Utils.getScaledValue(40);
	
	public ShopCart(BaseView bv, Waterfall waterfall) {
		super(bv);
		bv_ = bv;
        
        TextView blankView = new TextView(bv_);
        blankView.setId(123);
        blankView.setText(" ");
        RelativeLayout.LayoutParams blankPM = new RelativeLayout.LayoutParams(width_, blankH_);
        addView(blankView, blankPM);
        
		waterfall_ = waterfall;
		waterfall.setId(1);
		width_ = waterfall.getCmpWidth();
		height_ = waterfall.getCmpHeight() + blankH_;
        RelativeLayout.LayoutParams layoutpm = new RelativeLayout.LayoutParams(width_, height_);
        setLayoutParams(layoutpm);
        layoutpm.addRule(RelativeLayout.BELOW, 123);
        addView(waterfall, layoutpm);
        
        cart_ = new ImageView(bv);
        cart_.setImageResource(R.drawable.shopping_cart);
        cart_.setId(2);
        cart_.setScaleType(ScaleType.CENTER_INSIDE);
        cart_.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (bv_ instanceof DetailView) {
					DetailView bv = (DetailView)bv_;
					if (ShopCart.order.size() < 1) {
						Toast.makeText(bv_, "购物车为空,请重新选择", Toast.LENGTH_SHORT).show();
					} else {
						bv.dealWithLink("local://shopcart_page.xml", bv, false);
					}
				}
			}}
        );
        RelativeLayout.LayoutParams ivpm = new RelativeLayout.LayoutParams(Utils.getScaledValue(50), Utils.getScaledValue(50));
        ivpm.addRule(RelativeLayout.ALIGN_PARENT_LEFT, 1);
        addView(cart_, ivpm);
        
		buyNumView = new BadgeView(bv_, cart_);
		buyNumView.setTextColor(Color.WHITE);
		buyNumView.setBackgroundColor(Color.RED);
		buyNumView.setTextSize(12);
		
		// 当缓存数量大于0时，显示小球
		buyNum = ShopCart.order.size();
		if (buyNum > 0) {
			buyNumView.setText(buyNum + "");
			buyNumView.setBadgePosition(BadgeView.POSITION_TOP_RIGHT);
			buyNumView.show();
		}
//		waterfall_.adapter.setShopCart(this);
		waterfall_.setShopCart(this);
	}
	
	public void setAnimination(final View v, int[] start_location) {
		anim_mask_layout = null;
		anim_mask_layout = createAnimLayout();
		anim_mask_layout.addView(v);// 把动画小球添加到动画层
		final View view = addViewToAnimLayout(anim_mask_layout, v, start_location);
		int[] end_location = new int[2];// 这是用来存储动画结束位置的X、Y坐标
		cart_.getLocationInWindow(end_location);// cart是那个购物车

		// 计算位移
		int endX = 0 - start_location[0] + 40;// 动画位移的X坐标
		int endY = end_location[1] - start_location[1];// 动画位移的y坐标
		TranslateAnimation translateAnimationX = new TranslateAnimation(0, endX, 0, 0);
		translateAnimationX.setInterpolator(new LinearInterpolator());
		translateAnimationX.setRepeatCount(0);// 动画重复执行的次数
		translateAnimationX.setFillAfter(true);

		TranslateAnimation translateAnimationY = new TranslateAnimation(0, 0, 0, endY);
		translateAnimationY.setInterpolator(new AccelerateInterpolator());
		translateAnimationY.setRepeatCount(0);// 动画重复执行的次数
		translateAnimationX.setFillAfter(true);

		AnimationSet set = new AnimationSet(false);
		set.setFillAfter(false);
		set.addAnimation(translateAnimationY);
		set.addAnimation(translateAnimationX);
		set.setDuration(800);// 动画的执行时间
		view.startAnimation(set);
		// 动画监听事件
		set.setAnimationListener(new AnimationListener() {
			// 动画的开始
			@Override
			public void onAnimationStart(Animation animation) {
				v.setVisibility(View.VISIBLE);
			}

			@Override
			public void onAnimationRepeat(Animation animation) {
				// TODO Auto-generated method stub
			}

			// 动画的结束
			@Override
			public void onAnimationEnd(Animation animation) {
				v.setVisibility(View.GONE);
				buyNumView.setText(buyNum + "");
				buyNumView.setBadgePosition(BadgeView.POSITION_TOP_RIGHT);
				buyNumView.show();
			}
		});

	}
	
	/**
	 * 把item中勾选框状态置为未选中
	 */
	public void cancelItem() {
		buyNum--;// 让购买数量减1
		if (buyNum < 0) {
			buyNum = 0;
		}
		buyNumView.setText(buyNum + "");
		buyNumView.setBadgePosition(BadgeView.POSITION_TOP_RIGHT);
		buyNumView.show();
	}
	
	/**
	 * @Description: 创建动画层
	 * @param
	 * @return void
	 * @throws
	 */
	private ViewGroup createAnimLayout() {
		ViewGroup rootView = (ViewGroup) bv_.getWindow().getDecorView();
		LinearLayout animLayout = new LinearLayout(bv_);
		LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT);
		animLayout.setLayoutParams(lp);
		animLayout.setId(Integer.MAX_VALUE);
		animLayout.setBackgroundResource(android.R.color.transparent);
		rootView.addView(animLayout);
		return animLayout;
	}

	private View addViewToAnimLayout(final ViewGroup vg, final View view, int[] location) {
		int x = location[0];
		int y = location[1];
		LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
		lp.leftMargin = x;
		lp.topMargin = y;
		view.setLayoutParams(lp);
		return view;
	}

	@Override
	public String getContentText() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void setContentText(String text) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void shrinkWidth() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void cleanText() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mould() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void setCmpWidth(int width) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public int getCmpWidth() {
		// TODO Auto-generated method stub
		return width_;
	}

	@Override
	public void setCmpHeidht(int height) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public int getCmpHeight() {
		// TODO Auto-generated method stub
		return height_;
	}

	@Override
	public void setCssStyle(CssStyle style) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public View getCmpView() {
		// TODO Auto-generated method stub
		return this;
	}

	@Override
	public void setInTable(boolean inTable) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public boolean isInTable() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public CssStyle getCssStyle() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void releaseResource(View view) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void setForm(FormLayout formLayout) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public FormLayout getForm() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void setEncrypt(String encrypt) {
		// TODO Auto-generated method stub
		
	}
	
	public static void clearData(){
		// 在餐馆列表页面清空购物车缓存
		ShopCartList.amountList.clear();
		order.clear();
	}

}

